using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace QLKS
{
    public partial class frmNVXoaKH : Form
    {
        private DataTable _dSKH;

        internal DataTable DSKH
        {
            get { return _dSKH; }
            set { _dSKH = value; }
        }
        public frmNVXoaKH()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmNVXoaKH_Load(object sender, EventArgs e)
        {
            try
            {
                DSKH = BUS.XuLyKhachHang.TimDSKhachHang();
                dgvDSKH.DataSource = DSKH;

            }
            catch (System.Exception )
            {
            	
            }
          
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if (dgvDSKH.SelectedRows.Count > 0)
            {
                int index = dgvDSKH.SelectedRows[0].Index;

                DataRow dr = DSKH.Rows[index];

                // Xoa KH
                int MaKH = int.Parse(dr["MaKH"].ToString());

                if (!(BUS.XuLyKhachHang.XoaKhachHang(MaKH)))
                    MessageBox.Show("Loi xay ra. Khong the xoa khach hang. Kiem tra file CSDL");
                // Huy phieu thue cua KH nay
                List<DTO.CPHIEUTHUE> lstPT = new List<DTO.CPHIEUTHUE>();
                lstPT = BUS.XuLyPhieuThue.TimPhieuThueTheoMaKH(MaKH);
                foreach (DTO.CPHIEUTHUE pt in lstPT)
                {
                    // Xoa phieu thue
                    if(!(BUS.XuLyPhieuThue.XoaPhieuThue(pt.MaPhieu)))
                    {
                        MessageBox.Show("Loi xay ra. Khong the xoa cac phieu thue. Kiem tra file CSDL");
                    }
                    // Chuyen tinh trang cac phong thue cua KH lai 0
                    if (!(BUS.XuLyPhong.SuaTinhTrangPhong(0,pt.MaPhong)))
                    {
                        MessageBox.Show("Loi xay ra. Khong the sua tinh trang phong. Kiem tra file CSDL");
                    }
                }

            }
            else
                MessageBox.Show("Ban chua chon khach hang");

            frmNVXoaKH_Load(sender, e);
        }
    }
}